package cn.aeolia.config;

import cn.aeolia.Interceptor.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

//如果我们要扩展SpringMVC，官方建议我们这样去做
@Configuration
//@EnableWebMvc //这玩意就导入了一个类DelegatingWebMvcConfiguration 从容易中获取所有的webmvcconfig
public class MyMVCConfig implements WebMvcConfigurer {
    /**
     * 视图控制器
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");

    }

    /**
     * 地区语言解析器
     * @return
     */
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

    /**
     * 权限拦截器
     * 拦截未登录的请求
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> whiteList=new ArrayList<>();
        whiteList.add("/login");
        whiteList.add("/login.html");
        whiteList.add("/loginCheck");
        whiteList.add("/css/**");
        whiteList.add("/fonts/**");
        whiteList.add("/img/**");
        whiteList.add("/js/**");
        whiteList.add("/vendor/**");
        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns(whiteList);
    }
}
